package com.mantru.iotpot.Cook.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mantru.iotpot.DateClass.FirePowerDate;
import com.mantru.iotpot.DateClass.TasteData;
import com.mantru.iotpot.DateClass.TasteItem;
import com.mantru.iotpot.R;

import java.util.ArrayList;
import java.util.List;

public class SetTasteAdapter extends RecyclerView.Adapter<SetTasteAdapter.SetTasteViewHolder>{
    private Context context;
    private List<TasteData> tasteDataList;

    public SetTasteAdapter(List<TasteData> tasteDataList, Context context) {
        this.tasteDataList = tasteDataList;
        this.context = context;
    }
    @NonNull
    @Override
    public SetTasteAdapter.SetTasteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_set_taste, parent, false);
        return new SetTasteAdapter.SetTasteViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SetTasteAdapter.SetTasteViewHolder holder, int position) {
        List<TasteItem> tasteItem = tasteDataList.get(position).getSteepData();//获取当前的配置列表
        holder.title.setText(tasteDataList.get(position).getTitle1());
        Log.d("tasteItem","tasteItem.size： " + tasteItem.size());
        for (int i = 0;i < tasteItem.size();i++){
            final int index = i;
            final int currentPosition = position; // 使用局部变量保存 position
            holder.boxes[i].setVisibility(View.VISIBLE);//有多少数据就显示多少父容器出来
            holder.types[i].setText(tasteItem.get(i).getTitle2());//给类型text赋值
            holder.minus_texts[i].setText("-" + tasteItem.get(i).getStepValue() + tasteItem.get(i).getUnit());//给减步长赋值
            holder.add_texts[i].setText("+" + tasteItem.get(i).getStepValue() + tasteItem.get(i).getUnit());//给加步长赋值
            holder.seekBars[i].setMax(Integer.parseInt(tasteItem.get(i).getMaxValue()));//获取最大值
            holder.seekBars[i].setProgress(Integer.parseInt(tasteItem.get(i).getValue()));//获取当前的初始值
            holder.Values[i].setText(tasteItem.get(i).getValue());
            Log.d("tasteItem","i: " + i);
            Log.d("tasteItem","Step : " + tasteItem.get(i).getStepValue() + " max: "
                    + Integer.parseInt(tasteItem.get(i).getMaxValue()) + " Value: " + Integer.parseInt(tasteItem.get(i).getValue()));
            holder.seekBars[i].setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                private int roundedProgress;
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    if (fromUser) {
                        // 计算舍入后的进度值
                        int stepValue = Integer.parseInt(tasteItem.get(index).getStepValue());
                        roundedProgress = (int) Math.round((float) progress / stepValue) * stepValue;
                        roundedProgress = Math.max(0, Math.min(roundedProgress, seekBar.getMax()));

                        // 立即更新数据源和UI
                        tasteItem.get(index).setValue(String.valueOf(roundedProgress));
                        holder.Values[index].setText(String.valueOf(roundedProgress)); // 直接更新文本
                        set_taste_updateBubble(holder, currentPosition, index); // 更新气泡位置
                    }
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    int currentPosition = holder.getAdapterPosition();
                    if (roundedProgress == 0){
                        tasteItem.get(index).setValue(String.valueOf(roundedProgress + 1));//如果数值为0，则默认让他的值设置为1
                    }else {
                        tasteItem.get(index).setValue(String.valueOf(roundedProgress));
                    }
                    Log.d("tasteItem","tasteItem" + (currentPosition+1) + ": " + roundedProgress);
                    set_taste_updateBubble(holder, currentPosition, index); // 更新气泡位置
                }
            });

            holder.add_imgs[i].setOnClickListener(v -> {
                int currentValue = Integer.parseInt(tasteItem.get(index).getValue());
                int stepValue = Integer.parseInt(tasteItem.get(index).getStepValue());
                int maxValue = Integer.parseInt(tasteItem.get(index).getMaxValue());
                // 增加进度
                int newValue = currentValue + stepValue;
                newValue = Math.min(newValue, maxValue); // 确保不超过最大值
                holder.seekBars[index].setProgress(newValue);
                tasteItem.get(index).setValue(String.valueOf(newValue));
                Log.d("tasteItem","tasteItem" + (index+1) + ": " + newValue);
                set_taste_updateBubble(holder, currentPosition, index); // 更新气泡位置
            });

            holder.minus_imgs[i].setOnClickListener(v -> {
                int currentValue = Integer.parseInt(tasteItem.get(index).getValue());
                int stepValue = Integer.parseInt(tasteItem.get(index).getStepValue());
                // 减少进度
                int newValue = currentValue - stepValue;
                newValue = Math.max(newValue, 0); // 确保不超过最大值
                holder.seekBars[index].setProgress(newValue);
                if (newValue == 0){
                    tasteItem.get(index).setValue(String.valueOf(newValue + 1));//如果数值为0，则默认让他的值设置为1
                }else {
                    tasteItem.get(index).setValue(String.valueOf(newValue));
                }
                Log.d("tasteItem","tasteItem" + (index+1) + ": " + newValue);
                set_taste_updateBubble(holder, currentPosition, index); // 更新气泡位置
            });
        }
    }

    @Override
    public int getItemCount() {
        return tasteDataList.size();
    }

    public static class SetTasteViewHolder extends RecyclerView.ViewHolder{
        private final RelativeLayout[] boxes;//用于存放所有喷泵父容器的数组
        private final TextView[] types;//用于存放所有类型text的数组
        private final TextView[] minus_texts;//用于存放所有减小步长text的数组
        private final ImageView[] minus_imgs;//用于存放所有减小图标的数组
        private final SeekBar[] seekBars;
        private final ImageView[] add_imgs;
        private final TextView[] add_texts;
        private final RelativeLayout[] seekBar_boxs;//seekbar父容器数组
        private final FrameLayout[] Value_boxs;//气泡容器数组
        private final TextView[] Values;//气泡的初始值的数组
        TextView title;//标题
        RelativeLayout box1,box2,box3,box4,box5,box6,box7,box8,box9,box10,box11;//喷泵
        TextView type1,type2,type3,type4,type5,type6,type7,type8,type9,type10,type11;//喷泵名称
        //减小步长的text
        TextView minus_text1,minus_text2,minus_text3,minus_text4,minus_text5,minus_text6,minus_text7,minus_text8,minus_text9,minus_text10,minus_text11;
        //减小的图标
        ImageView minus_img1,minus_img2,minus_img3,minus_img4,minus_img5,minus_img6,minus_img7,minus_img8,minus_img9,minus_img10,minus_img11;
        SeekBar seekbar1,seekbar2,seekbar3,seekbar4,seekbar5,seekbar6,seekbar7,seekbar8,seekbar9,seekbar10,seekbar11;//托拉杆
        //增加的图标
        ImageView add_img1,add_img2,add_img3,add_img4,add_img5,add_img6,add_img7,add_img8,add_img9,add_img10,add_img11;
        //增加步长的text
        TextView add_text1,add_text2,add_text3,add_text4,add_text5,add_text6,add_text7,add_text8,add_text9,add_text10,add_text11;
        RelativeLayout seekBar_box1,seekBar_box2,seekBar_box3,seekBar_box4,seekBar_box5,seekBar_box6,seekBar_box7,seekBar_box8,seekBar_box9,seekBar_box10,seekBar_box11;
        FrameLayout Value_box1,Value_box2,Value_box3,Value_box4,Value_box5,Value_box6,Value_box7,Value_box8,Value_box9,Value_box10,Value_box11;
        TextView Value1,Value2,Value3,Value4,Value5,Value6,Value7,Value8,Value9,Value10,Value11;
        public SetTasteViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.set_taste_adapter_box_name);
            box1 = itemView.findViewById(R.id.set_taste_adapter_box1);
            box2 = itemView.findViewById(R.id.set_taste_adapter_box2);
            box3 = itemView.findViewById(R.id.set_taste_adapter_box3);
            box4 = itemView.findViewById(R.id.set_taste_adapter_box4);
            box5 = itemView.findViewById(R.id.set_taste_adapter_box5);
            box6 = itemView.findViewById(R.id.set_taste_adapter_box6);
            box7 = itemView.findViewById(R.id.set_taste_adapter_box7);
            box8 = itemView.findViewById(R.id.set_taste_adapter_box8);
            box9 = itemView.findViewById(R.id.set_taste_adapter_box9);
            box10 = itemView.findViewById(R.id.set_taste_adapter_box10);
            box11 = itemView.findViewById(R.id.set_taste_adapter_box11);
            boxes = new RelativeLayout[]{box1, box2, box3, box4, box5, box6, box7, box8, box9, box10, box11};

            type1 = itemView.findViewById(R.id.set_taste_adapter_box_type1);
            type2 = itemView.findViewById(R.id.set_taste_adapter_box_type2);
            type3 = itemView.findViewById(R.id.set_taste_adapter_box_type3);
            type4 = itemView.findViewById(R.id.set_taste_adapter_box_type4);
            type5 = itemView.findViewById(R.id.set_taste_adapter_box_type5);
            type6 = itemView.findViewById(R.id.set_taste_adapter_box_type6);
            type7 = itemView.findViewById(R.id.set_taste_adapter_box_type7);
            type8 = itemView.findViewById(R.id.set_taste_adapter_box_type8);
            type9 = itemView.findViewById(R.id.set_taste_adapter_box_type9);
            type10 = itemView.findViewById(R.id.set_taste_adapter_box_type10);
            type11 = itemView.findViewById(R.id.set_taste_adapter_box_type11);
            types = new TextView[]{type1,type2,type3,type4,type5,type6,type7,type8,type9,type10,type11};

            minus_text1 = itemView.findViewById(R.id.set_taste_adapter_minus_text1);
            minus_text2 = itemView.findViewById(R.id.set_taste_adapter_minus_text2);
            minus_text3 = itemView.findViewById(R.id.set_taste_adapter_minus_text3);
            minus_text4 = itemView.findViewById(R.id.set_taste_adapter_minus_text4);
            minus_text5 = itemView.findViewById(R.id.set_taste_adapter_minus_text5);
            minus_text6 = itemView.findViewById(R.id.set_taste_adapter_minus_text6);
            minus_text7 = itemView.findViewById(R.id.set_taste_adapter_minus_text7);
            minus_text8 = itemView.findViewById(R.id.set_taste_adapter_minus_text8);
            minus_text9 = itemView.findViewById(R.id.set_taste_adapter_minus_text9);
            minus_text10 = itemView.findViewById(R.id.set_taste_adapter_minus_text10);
            minus_text11 = itemView.findViewById(R.id.set_taste_adapter_minus_text11);
            minus_texts = new TextView[]{minus_text1,minus_text2,minus_text3,minus_text4,minus_text5,minus_text6,minus_text7,minus_text8,minus_text9,minus_text10,minus_text11};

            minus_img1 = itemView.findViewById(R.id.set_taste_adapter_minus_img1);
            minus_img2 = itemView.findViewById(R.id.set_taste_adapter_minus_img2);
            minus_img3 = itemView.findViewById(R.id.set_taste_adapter_minus_img3);
            minus_img4 = itemView.findViewById(R.id.set_taste_adapter_minus_img4);
            minus_img5 = itemView.findViewById(R.id.set_taste_adapter_minus_img5);
            minus_img6 = itemView.findViewById(R.id.set_taste_adapter_minus_img6);
            minus_img7 = itemView.findViewById(R.id.set_taste_adapter_minus_img7);
            minus_img8 = itemView.findViewById(R.id.set_taste_adapter_minus_img8);
            minus_img9 = itemView.findViewById(R.id.set_taste_adapter_minus_img9);
            minus_img10 = itemView.findViewById(R.id.set_taste_adapter_minus_img10);
            minus_img11 = itemView.findViewById(R.id.set_taste_adapter_minus_img11);
            minus_imgs = new ImageView[]{minus_img1,minus_img2,minus_img3,minus_img4,minus_img5,minus_img6,minus_img7,minus_img8,minus_img9,minus_img10,minus_img11};

            seekbar1 = itemView.findViewById(R.id.set_taste_adapter_seekbar1);
            seekbar2 = itemView.findViewById(R.id.set_taste_adapter_seekbar2);
            seekbar3 = itemView.findViewById(R.id.set_taste_adapter_seekbar3);
            seekbar4 = itemView.findViewById(R.id.set_taste_adapter_seekbar4);
            seekbar5 = itemView.findViewById(R.id.set_taste_adapter_seekbar5);
            seekbar6 = itemView.findViewById(R.id.set_taste_adapter_seekbar6);
            seekbar7 = itemView.findViewById(R.id.set_taste_adapter_seekbar7);
            seekbar8 = itemView.findViewById(R.id.set_taste_adapter_seekbar8);
            seekbar9 = itemView.findViewById(R.id.set_taste_adapter_seekbar9);
            seekbar10 = itemView.findViewById(R.id.set_taste_adapter_seekbar10);
            seekbar11 = itemView.findViewById(R.id.set_taste_adapter_seekbar11);
            seekBars = new SeekBar[]{seekbar1,seekbar2,seekbar3,seekbar4,seekbar5,seekbar6,seekbar7,seekbar8,seekbar9,seekbar10,seekbar11};

            add_img1 = itemView.findViewById(R.id.set_taste_adapter_add_img1);
            add_img2 = itemView.findViewById(R.id.set_taste_adapter_add_img2);
            add_img3 = itemView.findViewById(R.id.set_taste_adapter_add_img3);
            add_img4 = itemView.findViewById(R.id.set_taste_adapter_add_img4);
            add_img5 = itemView.findViewById(R.id.set_taste_adapter_add_img5);
            add_img6 = itemView.findViewById(R.id.set_taste_adapter_add_img6);
            add_img7 = itemView.findViewById(R.id.set_taste_adapter_add_img7);
            add_img8 = itemView.findViewById(R.id.set_taste_adapter_add_img8);
            add_img9 = itemView.findViewById(R.id.set_taste_adapter_add_img9);
            add_img10 = itemView.findViewById(R.id.set_taste_adapter_add_img10);
            add_img11 = itemView.findViewById(R.id.set_taste_adapter_add_img11);
            add_imgs = new ImageView[]{add_img1,add_img2,add_img3,add_img4,add_img5,add_img6,add_img7,add_img8,add_img9,add_img10,add_img11};

            add_text1 = itemView.findViewById(R.id.set_taste_adapter_add_text1);
            add_text2 = itemView.findViewById(R.id.set_taste_adapter_add_text2);
            add_text3 = itemView.findViewById(R.id.set_taste_adapter_add_text3);
            add_text4 = itemView.findViewById(R.id.set_taste_adapter_add_text4);
            add_text5 = itemView.findViewById(R.id.set_taste_adapter_add_text5);
            add_text6 = itemView.findViewById(R.id.set_taste_adapter_add_text6);
            add_text7 = itemView.findViewById(R.id.set_taste_adapter_add_text7);
            add_text8 = itemView.findViewById(R.id.set_taste_adapter_add_text8);
            add_text9 = itemView.findViewById(R.id.set_taste_adapter_add_text9);
            add_text10 = itemView.findViewById(R.id.set_taste_adapter_add_text10);
            add_text11 = itemView.findViewById(R.id.set_taste_adapter_add_text11);
            add_texts = new TextView[]{add_text1,add_text2,add_text3,add_text4,add_text5,add_text6,add_text7,add_text8,add_text9,add_text10,add_text11};

            seekBar_box1 = itemView.findViewById(R.id.set_taste_seekBar_box1);
            seekBar_box2 = itemView.findViewById(R.id.set_taste_seekBar_box2);
            seekBar_box3 = itemView.findViewById(R.id.set_taste_seekBar_box3);
            seekBar_box4 = itemView.findViewById(R.id.set_taste_seekBar_box4);
            seekBar_box5 = itemView.findViewById(R.id.set_taste_seekBar_box5);
            seekBar_box6 = itemView.findViewById(R.id.set_taste_seekBar_box6);
            seekBar_box7 = itemView.findViewById(R.id.set_taste_seekBar_box7);
            seekBar_box8 = itemView.findViewById(R.id.set_taste_seekBar_box8);
            seekBar_box9 = itemView.findViewById(R.id.set_taste_seekBar_box9);
            seekBar_box10 = itemView.findViewById(R.id.set_taste_seekBar_box10);
            seekBar_box11 = itemView.findViewById(R.id.set_taste_seekBar_box11);
            seekBar_boxs = new RelativeLayout[]{seekBar_box1,seekBar_box2,seekBar_box3,seekBar_box4,seekBar_box5,seekBar_box6,seekBar_box7,seekBar_box8,seekBar_box9,seekBar_box10,seekBar_box11};

            Value_box1 = itemView.findViewById(R.id.set_taste_adapter_Value_box1);
            Value_box2 = itemView.findViewById(R.id.set_taste_adapter_Value_box2);
            Value_box3 = itemView.findViewById(R.id.set_taste_adapter_Value_box3);
            Value_box4 = itemView.findViewById(R.id.set_taste_adapter_Value_box4);
            Value_box5 = itemView.findViewById(R.id.set_taste_adapter_Value_box5);
            Value_box6 = itemView.findViewById(R.id.set_taste_adapter_Value_box6);
            Value_box7 = itemView.findViewById(R.id.set_taste_adapter_Value_box7);
            Value_box8 = itemView.findViewById(R.id.set_taste_adapter_Value_box8);
            Value_box9 = itemView.findViewById(R.id.set_taste_adapter_Value_box9);
            Value_box10 = itemView.findViewById(R.id.set_taste_adapter_Value_box10);
            Value_box11 = itemView.findViewById(R.id.set_taste_adapter_Value_box11);
            Value_boxs = new FrameLayout[]{Value_box1,Value_box2,Value_box3,Value_box4,Value_box5,Value_box6,Value_box7,Value_box8,Value_box9,Value_box10,Value_box11};

            Value1 = itemView.findViewById(R.id.frie_power_adapter_Value1);
            Value2 = itemView.findViewById(R.id.frie_power_adapter_Value2);
            Value3 = itemView.findViewById(R.id.frie_power_adapter_Value3);
            Value4 = itemView.findViewById(R.id.frie_power_adapter_Value4);
            Value5 = itemView.findViewById(R.id.frie_power_adapter_Value5);
            Value6 = itemView.findViewById(R.id.frie_power_adapter_Value6);
            Value7 = itemView.findViewById(R.id.frie_power_adapter_Value7);
            Value8 = itemView.findViewById(R.id.frie_power_adapter_Value8);
            Value9 = itemView.findViewById(R.id.frie_power_adapter_Value9);
            Value10 = itemView.findViewById(R.id.frie_power_adapter_Value10);
            Value11 = itemView.findViewById(R.id.frie_power_adapter_Value11);
            Values = new TextView[]{Value1,Value2,Value3,Value4,Value5,Value6,Value7,Value8,Value9,Value10,Value11};
        }
    }

    public void set_taste_updateBubble(SetTasteViewHolder holder, int position, int index) {
        SeekBar seekBar = holder.seekBars[index];
        FrameLayout valueBox = holder.Value_boxs[index];
        TextView valueText = holder.Values[index];
        // 直接使用 SeekBar 的当前进度值（不再依赖数据源）
        int currentProgress = seekBar.getProgress();
        valueText.setText(String.valueOf(currentProgress));

        // 获取父容器的范围
        RelativeLayout parentLayout = holder.seekBar_boxs[index];

        // 确保父容器和 SeekBar 的宽度已初始化
        parentLayout.post(new Runnable() {
            @Override
            public void run() {
                int parentWidth = parentLayout.getWidth();
                int seekBarWidth = seekBar.getWidth();

                // 计算滑块的精确位置（使用进度计算，避免滑块偏移问题）
                int progress = seekBar.getProgress();
                int thumbPosition = seekBar.getThumbOffset() + (int) (progress * ((float) seekBarWidth / seekBar.getMax()));

                // 考虑布局方向（如 RTL 布局）
                if (seekBar.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
                    thumbPosition = parentWidth - thumbPosition;
                }

                // 计算气泡的水平位置（相对于父容器）
                int bubbleX = thumbPosition - valueBox.getWidth() / 2;

                // 确保气泡不会超出父容器的左右边界
                bubbleX = Math.max(0, Math.min(bubbleX, parentWidth - valueBox.getWidth()));

                // 气泡的垂直位置（固定在 SeekBar 的上方）
                int bubbleY = -valueBox.getHeight() - 10; // 调整偏移量

                // 更新气泡位置
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) valueBox.getLayoutParams();
                layoutParams.removeRule(RelativeLayout.CENTER_HORIZONTAL);
                layoutParams.leftMargin = bubbleX;
                layoutParams.topMargin = bubbleY;

                valueBox.setLayoutParams(layoutParams);
                valueBox.requestLayout(); // 强制刷新布局

                Log.d("updateBubble", "position: " + position + ", index: " + index + "\nbubbleX: " + bubbleX + "\nbubbleY: " + bubbleY);
            }
        });
    }

    public void updateVisibleBubbles(SetTasteViewHolder holder, int position) {
        List<TasteItem> tasteItem = tasteDataList.get(position).getSteepData();
        for (int i = 0; i < tasteItem.size(); i++) {
            holder.seekBars[i].setProgress(Integer.parseInt(tasteItem.get(i).getValue()));
            set_taste_updateBubble(holder, position, i);
        }
    }
}
